package GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import core.model.Usuario;
import core.thread.Mail;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;

public class panelRecordarPassword extends javax.swing.JPanel implements
		ActionListener {

	private static final int PLAIN_MESSAGE = 0;
	private static final int OK_CANCEL_OPTION = 0;

	public panelRecordarPassword() {
		initComponents();
	}

	private void initComponents() {

		bTitulo = new javax.swing.JButton();
		eNick = new javax.swing.JLabel();
		bEmail = new javax.swing.JLabel();
		tfNick = new javax.swing.JTextField();
		tfEmail = new javax.swing.JTextField();
		bEnviar = new javax.swing.JButton();
		bAtras = new javax.swing.JButton();
		botonSalir = new javax.swing.JButton();

		setBackground(new java.awt.Color(255, 255, 255));
		setPreferredSize(new java.awt.Dimension(610, 266));

		bTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png")); // NOI18N
		bTitulo.setBorder(null);
		bTitulo.setBorderPainted(false);
		bTitulo.setBackground(new java.awt.Color(255, 255, 255));
		bTitulo.addActionListener(this);

		eNick.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eNick.setForeground(new java.awt.Color(243, 15, 120));
		eNick.setText("E-MAIL");

		bEmail.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bEmail.setForeground(new java.awt.Color(243, 15, 120));
		bEmail.setText("NICK");

		tfNick.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		tfNick.setForeground(new java.awt.Color(102, 102, 102));

		tfEmail.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		tfEmail.setForeground(new java.awt.Color(102, 102, 102));

		bEnviar.setBackground(new java.awt.Color(255, 255, 255));
		bEnviar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bEnviar.setForeground(new java.awt.Color(243, 15, 120));
		bEnviar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonEnviar.png")); // NOI18N
		bEnviar.setBorder(null);
		bEnviar.addActionListener(this);

		bAtras.setBackground(new java.awt.Color(255, 255, 255));
		bAtras.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/boton_atras.png")); // NOI18N
		bAtras.setBorder(null);
		bAtras.addActionListener(this);

		botonSalir.setBackground(new java.awt.Color(255, 255, 255));
		botonSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		botonSalir.setBorderPainted(false);
		botonSalir.addActionListener(this);

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(119, 119, 119)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.TRAILING)
								.add(bEmail).add(eNick))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING,
										false)
								.add(tfNick,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										250, Short.MAX_VALUE).add(tfEmail))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE))
				.add(org.jdesktop.layout.GroupLayout.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.add(layout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(org.jdesktop.layout.GroupLayout.TRAILING,
												layout.createSequentialGroup()
														.add(0, 0,
																Short.MAX_VALUE)
														.add(bTitulo))
										.add(org.jdesktop.layout.GroupLayout.TRAILING,
												layout.createSequentialGroup()
														.add(bAtras)
														.addPreferredGap(
																org.jdesktop.layout.LayoutStyle.RELATED)
														.add(botonSalir)
														.add(127, 127, 127)
														.add(bEnviar)
														.add(0, 0,
																Short.MAX_VALUE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.addContainerGap()
						.add(bTitulo)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.TRAILING)
								.add(layout
										.createSequentialGroup()
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.BASELINE)
												.add(bEmail)
												.add(tfNick,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(28, 28, 28)
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.BASELINE)
												.add(eNick)
												.add(tfEmail,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.UNRELATED)
										.add(bEnviar,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												39,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(layout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(botonSalir).add(bAtras)))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
	}

	private javax.swing.JButton bAtras;
	private javax.swing.JLabel bEmail;
	private javax.swing.JButton bEnviar;
	private javax.swing.JLabel eNick;
	private javax.swing.JButton bTitulo;
	private javax.swing.JButton botonSalir;
	private javax.swing.JTextField tfEmail;
	private javax.swing.JTextField tfNick;
	private JFrame actual;
	private JPanel estePanel = this;

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelInicio());
			actual.setPreferredSize(new Dimension(610, 350));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bAtras) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelInicio());
			actual.setPreferredSize(new Dimension(610, 350));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == botonSalir) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(2));
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bEnviar) {
			VentanaManager ventanaManager=VentanaFactory.getInstance();
			Usuario a= ventanaManager.findUsuarioBy(tfNick.getText());
			if(tfEmail.getText().equals(a.getEmail()))
			{
				String texto=String.format("Nombre de Usuario: %s \n\nPassword: %s ", a.getNick(),a.getPassword());
				Mail email=new Mail(a.getEmail(),"Recordatorio de tu contraseña de Lista de la Compra Maker", texto);
				email.run();
				
	
			}
			else
			{
				Icon icono = new ImageIcon("src/GUI/resources/error.gif");
				
				
				
				JOptionPane.showOptionDialog(null, "El email es incorrecto",
				"Recordar password", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
				new Object[] { "ACEPTAR" }, null);
			}

			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelInicio());
			actual.setPreferredSize(new Dimension(610, 350));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}

	}
}
